package com.yang.moyu.meta.cms.service.impl;

import com.yang.moyu.meta.cms.mapper.CmsArticleMapper;
import com.yang.moyu.meta.cms.params.QryArticleListParam;
import com.yang.moyu.meta.cms.params.CmsArticleParam;
import com.yang.moyu.meta.cms.service.CmsArticleCategoryService;
import com.yang.moyu.meta.cms.service.CmsArticleService;
import com.yang.moyu.meta.cms.service.CmsArticleTagService;
import com.yang.moyu.meta.cms.vo.CmsArticleVo;
import com.yang.moyu.meta.common.util.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by 10296 on 2021/12/30
 *
 * @Description: 文章管理服务类
 */
@Service
public class CmsArticleServiceImpl implements CmsArticleService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CmsArticleMapper articleMapper;
    @Autowired
    private CmsArticleTagService articleTagService;
    @Autowired
    private CmsArticleCategoryService articleCategoryService;

    @Override
    public ApiResult addCmsArticle(CmsArticleParam articleParam) {

        //判空
        if (StringUtils.isEmpty(articleParam.getArticleTitle())) {
            return ApiResult.fail("标题不能为空！");
        }
        if (StringUtils.isEmpty(articleParam.getContent())) {
            return ApiResult.fail("内容不能为空！");
        }
        if (StringUtils.isEmpty(articleParam.getId())) {
            return ApiResult.fail("创建人不能为空!");
        }

        articleMapper.addCmsArticle(articleParam);
        String articleId = articleParam.getId();
        String articleTag = articleParam.getArticleTag();
        if (StringUtils.isNotEmpty(articleTag)) {
            String[] split = articleTag.split(",");
            for (String tag : split) {
                Map<String, Object> tagParamsMap = new HashMap<>();
                tagParamsMap.put("articleId", articleId);
                tagParamsMap.put("tagId", tag);
                if (StringUtils.isNotEmpty(tag)) {
                    articleTagService.saveCmsArticleTag(tagParamsMap);
                }
            }
        }
        String articleCategory = articleParam.getArticleCategory();
        if (StringUtils.isNotEmpty(articleCategory)) {
            Map<String, Object> paramsMap = new HashMap<>();
            paramsMap.put("articleId", articleId);
            paramsMap.put("categoryId", articleCategory);

            articleCategoryService.saveArticleCategory(paramsMap);
        }

        return ApiResult.ok("添加成功!");
    }

    @Override
    public ApiResult updateCmsArticle(CmsArticleParam articleParam) {
        return null;
    }

    @Override
    public ApiResult qryCmsArticleList(QryArticleListParam articleParam) {
        List<CmsArticleVo> cmsArticleVos = articleMapper.qryCmsArticleList(articleParam);

        return ApiResult.ok(cmsArticleVos);
    }

    @Override
    public ApiResult delCmsArticle(CmsArticleParam articleParam) {
        return null;
    }
}
